import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:getx_scaffold_ohos/getx_scaffold_ohos.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'recommend_logic.dart';

/// 首页 -- 推荐
class RecommendPage extends StatefulWidget{
  const RecommendPage({super.key});

  @override
  State<RecommendPage> createState() => _RecommendPageState();
}

class _RecommendPageState extends State<RecommendPage> with AutomaticKeepAliveClientMixin{
  final RecommendLogic controller = Get.put(RecommendLogic());

  Widget _buildBanner() {
    if (controller.bannerList.isEmpty) {
      return const Center(child: CircularProgressIndicator());
    } else {
      return Swiper(
        key: ValueKey(controller.bannerList.hashCode),
        autoplay: true,
        autoplayDelay: 3000,
        itemBuilder: (context, index) {
          return TDImage(
            imgUrl: controller.bannerList[index].imagePath,
            type: TDImageType.roundedSquare,
            loadingWidget: RotationTransition(
              turns: const AlwaysStoppedAnimation(0.5),
              alignment: Alignment.center,
              child: TDCircleIndicator(
                size: 18.r,
                lineWidth: 3.r,
              ),
            ),
            errorWidget: const TDText(
              '加载失败',
              forceVerticalCenter: true,
              fontWeight: FontWeight.w500,
              textColor: Colors.grey,
            ),
          );
        },
        itemCount: controller.bannerList.length,
        pagination: const SwiperPagination(),
        viewportFraction: 0.8,
        scale: 0.8,
      );
    }
  }

  Widget _buildWxChapters() {
    if (controller.wxChapterList.isEmpty) {
      return const SizedBox();
    } else {
      return ListView.builder(
        key: ValueKey(controller.wxChapterList.hashCode),
        scrollDirection: Axis.horizontal,
        shrinkWrap: true,
        physics: const ClampingScrollPhysics(),
        itemBuilder: (context, index) {
          return Container(
            padding: EdgeInsets.all(8.r),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                TDImage(
                  width: 60.r,
                  height: 60.r,
                  imgUrl: 'https://picsum.photos/100?random=$index',
                  type: TDImageType.circle,
                  fit: BoxFit.cover,
                  loadingWidget: RotationTransition(
                    turns: const AlwaysStoppedAnimation(0.5),
                    alignment: Alignment.center,
                    child: TDCircleIndicator(
                      size: 18.r,
                      lineWidth: 3.r,
                    ),
                  ),
                  errorWidget: const TDText(
                    '加载失败',
                    forceVerticalCenter: true,
                    fontWeight: FontWeight.w500,
                    textColor: Colors.grey,
                  ),
                ),
                Container(
                  alignment: Alignment.center,
                  child: TDText(
                    controller.wxChapterList[index].name,
                    forceVerticalCenter: true,
                  ),
                )
              ],
            ),
          );
        },
        itemCount: controller.wxChapterList.length,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<RecommendLogic>(
      id: 'recommend',
      init: controller,
      builder: (RecommendLogic controller) {
        return SmartRefresher(
          enablePullUp: true,
          enablePullDown: true,
          controller: controller.refreshController,
          onRefresh: () => controller.onRefresh(),
          onLoading: () => controller.loadMore(),
          child: CustomScrollView(
            slivers: [
              /// banner
              SliverToBoxAdapter(
                child: Container(
                  width: double.infinity,
                  height: 200.h,
                  margin: EdgeInsets.only(top: 10.h, bottom: 10.h),
                  child: _buildBanner(),
                ),
              ),

              /// 文章列表
              SliverList.separated(
                itemBuilder: (BuildContext context, int index){
                  return GestureDetector(
                    onTap: (){},
                    child: Container(
                      height: 200.h,
                      width: double.infinity,
                      decoration: BoxDecoration(
                        color: index % 2 == 0 ? Colors.blue : Colors.red,
                        borderRadius: BorderRadius.circular(12.r),
                      ),
                      margin: EdgeInsets.symmetric(horizontal: 15.w),
                    ),
                  );
                },
                separatorBuilder: (BuildContext context, int index){
                  return SizedBox(height: 10.h,width: double.infinity);
                },
                itemCount: controller.dataList.length,
              )
            ],
          ),
        );
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}
